package com.qding.park.manager.timer.schedule.job;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Service;

import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.manager.pojo.TbCarlock;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;
import com.qding.park.manager.timer.ParkTimerApplication;
import com.qding.park.manager.timer.TimerContext;
import com.qding.park.manager.timer.conf.TaskConfig;

/**
 * @ClassName CarLockCheckJob
 * @Description TODO(锁车单超时检查)
 * @author Administrator
 * @Date 2016年9月15日 下午3:06:15
 * @version 1.0.0
 */
@Service
@DisconfUpdateService( classes={TaskConfig.class},itemKeys = { "task.CarLockCheckJob.name" ,"task.CarLockCheckJob.taskJobClass","task.CarLockCheckJob.cronTime"})
public class CarLockCheckJob extends AbstractJob implements IDisconfUpdate{
	private static final Log _log = LogFactory.getLog(CarLockCheckJob.class);

	@Override
	public void execJob(JobExecutionContext context) {
		JobKey jobKey = context.getJobDetail().getKey();
		
//		_log.info("CarLockCheckJob starting"+context.getTrigger().);
		@SuppressWarnings("static-access")
		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(CacheServiceFacade.class);
		
//		// 得到redis client
//		JedisClient jedisClient = ParkTimerApplication.ctxUtil.getApplicationContext().getBean(JedisClient.class);
//		
//		//获取锁车单的　　监控列表
//		Map<String, String> mset=jedisClient.hgetAll(CacheKeyThree.MONITOR_CARLOCK_KEY);
		List<TbCarlock> carLocks=cacheServiceFacade.carLock_timeout_monitor_list();
		for(TbCarlock tbCarlock:carLocks){
			Date endTime=tbCarlock.getEndTime();
			Date now=new Date();
			if(now.getTime()>endTime.getTime()){
				QueueMsg  msg=new QueueMsg();
				msg.setEventType(QueueTypeEnum.CARLOCK_TIMEOUT.getType());
				TbCarlock data=new TbCarlock();
				data.setId(tbCarlock.getId());
				msg.setData(data);
				//入队列
				cacheServiceFacade.msg_queue_push(msg);
				//删除监控
				cacheServiceFacade.carLock_timeout_monitor_delete(tbCarlock.getId());
			}
		}
		
		_log.info("锁车单超时检查: " + jobKey + " executing at " + new Date());

	}

	
	@Override
	public void reload() throws Exception {
		_log.info(TaskConfig.getTask_CarLockCheckJob_cronTime());
		String taskName=TaskConfig.getTask_CarLockCheckJob_name();
		String cronTime=TaskConfig.getTask_CarLockCheckJob_cronTime();
		super.reloadTask(taskName,cronTime);
		
	}
}
